import myAxios from "@/request";
import { ApiResponse, UserInfo } from "@/types";

export interface ImageItem {
  file: File;
}

export interface blogRequest {
  id: number;
  userId: number;
  content: string;
  mediaList?: ImageItem[]; // 博客图片数组
  username: string;
  avatar: string;
  likesCount: number;
  liked: boolean;
  commentsCount: number;
  createdAt: string;
  privateKey: number;
  isMyself: boolean;
  isFollowed: boolean;
}

export interface blogCreateRequest {
  content: string;
}

/**
 * 获取热门博客列表
 * @param params
 * @returns
 */
export const getHotBlogList = async () => {
  const response = await myAxios.get<ApiResponse<blogRequest[]>>("/blog/hot");
  return response.data;
};

/**
 * 获取我的博客列表
 * @returns
 */
export const getMyBlogList = async () => {
  const response = await myAxios.get<ApiResponse<blogRequest[]>>(
    "/blog/myBlog"
  );
  return response.data;
};

/**
 * 获取用户博客列表
 * @param params
 * @returns
 */
export const getUserBlogList = async (params: number) => {
  const response = await myAxios.get<ApiResponse<blogRequest[]>>(
    `/blog/user/${params}`
  );
  return response.data;
};

/**
 * 获取博客详情
 * @param params
 * @returns
 */
export const getBlog = async (params: number) => {
  const response = await myAxios.get<ApiResponse<blogRequest>>(
    `/blog/getBlog/${params}`
  );
  return response.data;
};

/**
 * 获取关注的博客列表
 * @returns
 */
export const getFollowBlogList = async () => {
  const response = await myAxios.get<ApiResponse<blogRequest[]>>(
    "/blog/followersBlog"
  );
  return response.data;
};

/**
 * 获取搜索的博客列表
 * @returns
 * @param key
 */
export const getSearchBlogList = async (key: string) => {
  const response = await myAxios.get<ApiResponse<blogRequest[]>>(
    `/blog/search?key=${key}`
  );
  return response.data;
};

/**
 * 创建博客
 * @param blogCreateDTO
 * @param images
 * @param privateKey
 * @returns
 */
export const createBlog = async (
  content: string,
  privateKey: number,
  images: ImageItem[]
) => {
  const formData = new FormData();

  // 添加 blog JSON 字符串
  const blogData = {
    content,
    privateKey,
  };
  formData.append("blog", JSON.stringify(blogData));

  // 添加图片文件
  if (images && images.length > 0) {
    for (let i = 0; i < images.length; i++) {
      formData.append("images", images[i].file);
    }
  }

  try {
    const response = await myAxios.post("/blog/create", formData, {
      withCredentials: true, // 保证携带 Cookie（如 token）
    });
    console.log("✅ 博客创建成功:", response.data);
    return response.data;
  } catch (error) {
    console.error("❌ 创建博客失败:", error);
    throw error;
  }
};

export const likeBlog = async (params: number) => {
  return await myAxios.request({
    url: `/blog/like/${params}`,
    method: "PUT",
  });
};

export const isLike = async (params: number) => {
  return await myAxios.request({
    url: `/blog/like/${params}`,
    method: "GET",
    data: {},
  });
};

/**
 * 修改博客私密密钥
 * @param params
 * @param pk
 */
export const updatePk = async (params: number, pk: number) => {
  return await myAxios.request({
    url: `/blog/updatePk/${params}`,
    method: "PUT",
    data: {
      privateKey: pk,
    },
  });
};

export const deleteBlog = async (params: number) => {
  return await myAxios.request({
    url: `/blog/delete/${params}`,
    method: "DELETE",
  });
};
